package com.intellekt.ecertws.domain;

import java.util.Calendar;

import javax.sql.DataSource;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;

public class JdbcDao {

	protected NamedParameterJdbcTemplate namedParameterJdbcTemplate;
    protected JdbcTemplate jdbcTemplate;
    protected NamedParameterJdbcTemplate namedParameterJdbcTemplateVU;
    protected JdbcTemplate jdbcTemplateVU;
	protected static Calendar aCalendar = Calendar.getInstance();
	protected static final Log log = LogFactory.getLog(JdbcDao.class);

	/**
	 * ORA-12899: el valor es demasiado grande para la columna
	 */
	public static final int ORA_12899 = 12899;

	/**
	 * ORA-00904: identificador no valido
	 */
	public static final int ORA_00904 = 904;

	/**
	 * ORA-01438: valor mayor que el que permite la precision especificada para
	 * esta columna
	 */
	public static final int ORA_01438 = 1438;

	/**
	 * ORA-01400: no se puede realizar una insercion NULL
	 */

	public static final int ORA_01400 = 1400;

	/**
	 * ORA-00001: restriccion unica
	 */

	public static final int ORA_00001 = 00001;

	/**
	 * ORA-01847: el dia del mes debe estar entre 1 y el ultimo dia del mes
	 */
	public static final int ORA_01847 = 1847;

	public static int[] ERRORES_CONTINUAR = new int[6];

	static {
		ERRORES_CONTINUAR[0] = ORA_12899;
		ERRORES_CONTINUAR[1] = ORA_00904;
		ERRORES_CONTINUAR[2] = ORA_01438;
		ERRORES_CONTINUAR[3] = ORA_01400;
		ERRORES_CONTINUAR[4] = ORA_00001;
		ERRORES_CONTINUAR[5] = ORA_01847;
	}

	protected DataSource dataSource;
    protected DataSource dataSourceVU;
    
    public final void setDataSource(DataSource dataSource) {
        this.namedParameterJdbcTemplate = new NamedParameterJdbcTemplate(dataSource);
        this.jdbcTemplate = new JdbcTemplate(dataSource);
        
        this.dataSource = dataSource;
    }

    public final void setDataSourceVU(DataSource dataSourceVU) {
        this.namedParameterJdbcTemplateVU = new NamedParameterJdbcTemplate(dataSourceVU);
        this.jdbcTemplateVU = new JdbcTemplate(dataSourceVU);
        
        this.dataSourceVU = dataSourceVU;
    }

	protected void checkAffected(int affected) {
		if (affected > 1) {
			throw new RuntimeException("La operacion afecto mas de 1 registro.");
		}
		if (affected < 1) {
			throw new RuntimeException("La operacion no afecto ningun registro.");
		}
	}

	protected String debugVar(String name, Object var) {
		return new StringBuffer(name).append(" => [ ").append(var).append(" ] ").toString();
	}

	protected String parseBoolean(boolean value) {
		if (value == true) {
			return "S";
		} else {
			return "N";
		}
	}

	protected String parseHour(String hh, String mm) {
		if (StringUtils.isNotBlank(hh) && StringUtils.isNotBlank(mm)) {
			return new StringBuffer(hh).append(":").append(mm).toString();
		} else {
			return null;
		}
	}

	protected final String toUpper(String value) {
		if (value == null) {
			return null;
		}
		if (StringUtils.isBlank(value)) {
			return "";
		}
		return value.toUpperCase();
	}

	protected final String toUpperTrim(String value) {
		if (value == null) {
			return null;
		}
		if (StringUtils.isBlank(value)) {
			return "";
		}
		return value.toUpperCase().trim();
	}

}
